/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.utils.swing;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * Class to derive from in order to create mouse listeners that intercept mouse events before other "built-in"
 * listeners see the events. Used for example in JLists in order flexibly prevent list selection on mouse pressed.
 * Simply override the desired methods and either call or don't call super.mouseXXX(...) in order to have the other
 * listeners see the event.
 */
public abstract class MouseInterceptor implements MouseListener {
    private final MouseListener[] otherListeners;

    /**
     * Creates and attached a new MouseInterceptor for the given component.
     *
     * @param component the component to capture mouse events for
     */
    protected MouseInterceptor(Component component) {
        otherListeners = component.getMouseListeners();
        for (MouseListener l : otherListeners) {
            component.removeMouseListener(l);
        }
        component.addMouseListener(this);
    }

    public void mouseClicked(MouseEvent e) {
        for (MouseListener otherListener : otherListeners) {
            otherListener.mouseClicked(e);
        }
    }

    public void mousePressed(MouseEvent e) {
        for (MouseListener otherListener : otherListeners) {
            otherListener.mousePressed(e);
        }
    }

    public void mouseReleased(MouseEvent e) {
        for (MouseListener otherListener : otherListeners) {
            otherListener.mouseReleased(e);
        }
    }

    public void mouseEntered(MouseEvent e) {
        for (MouseListener otherListener : otherListeners) {
            otherListener.mouseEntered(e);
        }
    }

    public void mouseExited(MouseEvent e) {
        for (MouseListener otherListener : otherListeners) {
            otherListener.mouseExited(e);
        }
    }

}
